﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Kunlun.Models;
using Kunlun.ViewModels;
namespace Kunlun.BusinessLogic {
	public class KunlunOrder {
		KunLunEntities entities = new KunLunEntities();
		public string CreateOrder(string addressid, string sendid, string payid, string invoice)
		{
			DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
			string mCode = entities.Order.Where(o => o.BeginDate >= dt).Select(o => o.Oid).Max();


			if (string.IsNullOrEmpty(mCode))
			{
				mCode = "000000000";
			}

			string orderid = DateTime.Now.ToString("yyMMdd") + (Convert.ToInt32(mCode.Substring(6)) + 1).ToString("0000");
			try
			{
				int userid = int.Parse(HttpContext.Current.Request.Cookies.Get("userId").Value);
				int deliverid = int.Parse(sendid);
				int addid = int.Parse(addressid);
				var user = entities.Consumers.FirstOrDefault(u => u.ID == userid);

				List<UserCartViewModels> list = KunlunCarts.GetCars(true);

				var address = entities.Address.FirstOrDefault(a => a.AddressID == addid);
				Order order = new Order();
				order.Oid = orderid;
				order.AddressID = int.Parse(addressid);
				order.BeginDate = DateTime.Now;
				order.BeginIP = HttpContext.Current.Request.UserHostAddress;
				order.Paymethodid = int.Parse(payid);
				order.Connections = user.Phone;
				order.AddressInfo = address.Provice.proviceName + address.City.cityName + address.Area.areaName + address.AddressPlace;
				order.Scored = 0;
				order.Scoring = 0;
				order.IsEnd = true;
				order.Status = 0;
				order.UserID = userid;
				order.Invoice = invoice;
				order.Price = KunlunCarts.Allprice;
				var deliver = entities.DeliveType.FirstOrDefault(d => d.ID == deliverid);
				order.Sendprice = deliver.Fees;
				order.SendType = deliverid;
				order.LastUpdateTime = DateTime.Now;
				entities.Order.Add(order);
				foreach (UserCartViewModels cart in list)
				{
					Orderdetail detail = new Orderdetail();
					detail.BeginDate = DateTime.Now;
					detail.Oneprice = cart.Prices;
					detail.OrderID = orderid;
					detail.ProductID = cart.PID;
					detail.ProductName = cart.Product;
					detail.Quantity = cart.Numbers;
					detail.Remark = "";
					detail.ItemNumber = cart.Index;
					detail.Types = cart.Types;
					if (cart.Attributes != 0)
					{
						detail.ProAttrID = cart.Attributes;
					}
					if (cart.Types == 0 && cart.Level != 0)
					{
						detail.Remark = "卡纸" + cart.Level + " 距离:" + (PaperSize)cart.Size;
					}
					else if (cart.Types != 0)
					{
						detail.Remark = ((FrameType)(cart.Types)).ToString();
					}
					entities.Orderdetail.Add(detail);
				}
				entities.SaveChanges();







			}
			catch (Exception ex)
			{
				WebLog.ErrorLog(ex);
			}
			return orderid;
		}
		private string ToStr(int str, int l) {
			string s = str.ToString();
			for (int i = 0; i < l - s.Length; i++) {
				s = "0" + s;
			}
			return s;
		}
	}

}
